﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using DAL = KC.DataAccess.Meta;

namespace KC.Business.Meta
{
	public abstract class JobBase
	{
		public virtual void PreProcess(ref DAL.MetaDataContext dc, ref DAL.JobRun jr)
		{
			UpdateStatus(ref dc, ref jr, Meta.JobStatus.PreProcessing);
		}
		public virtual void Process(ref DAL.MetaDataContext dc, ref DAL.JobRun jr)
		{
			UpdateStatus(ref dc, ref jr, Meta.JobStatus.Processing);
		}
		public virtual void PostProcess(ref DAL.MetaDataContext dc, ref DAL.JobRun jr)
		{
			UpdateStatus(ref dc, ref jr, Meta.JobStatus.PostProcessing);
		}
		public void Complete(ref DAL.MetaDataContext dc, ref DAL.JobRun jr, bool isSuccess)
		{
			UpdateStatus(ref dc, ref jr, isSuccess ? Meta.JobStatus.Completed : Meta.JobStatus.Failed);
		}
		private void UpdateStatus(ref DAL.MetaDataContext dc, ref DAL.JobRun jr, Meta.JobStatus status)
		{
			Debug.WriteLine("JobRun " + jr.Id.ToString() + ": " + status.ToString());
			DAL.Meta.UpdateStatus(ref dc, ref jr, status.ToString(), status == Meta.JobStatus.Completed || status == Meta.JobStatus.Failed);
		}
	}
}
